package android.FiltrosJava;

public class UniformNoise extends PointFilter{
	protected static double sigma = 20;
	protected static int N = 256;
	protected double[] distrib = null;
	static protected double[] computeUniformNoise(){		
		double[] g = new double[N];
		
		int min = (int)(- Math.ceil((N - 1) / 2.0f));				
		double total = 0.0f;
		for (int y = 0; y < N; y++){			
			g[y] = (1.0f/(sigma * Math.sqrt(20.0f * Math.PI))) * Math.exp((-1.0f/2.0f) * (min / sigma) * (min / sigma));
			total += g[y];
			min++;				
		}
		g[0] = g[0] /total;
		for (int y = 1; y < N; y++){
			g[y] = g[y-1] + g[y]/total;						
		}
		
		return g;
	}
	
	protected double percNoise;
	public UniformNoise(double percentNoise){
		this.percNoise = percentNoise;
		if (this.percNoise > 1.0f){
			this.percNoise = percentNoise % 100;
			this.percNoise = (float) (this.percNoise /100.0f);
		}
		
		distrib = computeUniformNoise();			 
	}
	
	protected int getUniformNoise(){		
		double soma = 0.0f;
		double d = Math.random();
		int y = -1;
		while ((y < N) && (soma < d)){
			y++;
			soma += distrib[y];			
		}
		if (y == -1)
			y++;
		
		return y;
	}
	
	public int[] computeFilter(int px, int py, int[] imageData){
		double p = Math.random();		
		if (p <= this.percNoise){
			if ((imageData[0] == imageData[1]) && (imageData[1] == imageData[2])){
				int cor = getUniformNoise();
				imageData[0] = cor;
				imageData[1] = cor;
				imageData[2] = cor;	
			}else{
				imageData[0] = getUniformNoise();
				imageData[1] = getUniformNoise();
				imageData[2] = getUniformNoise();
			}				
		}		
		
		return imageData;
	}

	@Override
	public int[] applyFilter(int[] pixels) {
		// TODO Auto-generated method stub
		return null;
	}
}